package com.wy.framework.actions.permission;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.services.permission.PermissionManageService;
import com.wy.framework.services.permission.RoleManageService;

/**
 * 项目名称：openportal
 * 类名称：PermissionAction
 * 类描述：权限信息管理
 * 创建人：张旭星
 * 创建时间：2015-12-28 下午7:49:04
 * 修改人：张旭星
 * 修改时间：2015-12-28 下午7:49:04
 * 版本信息：@version 1.0
 * Copyright (c) 2015万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@ParentPackage("json")
@Namespace("/manage/permission")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class PermissionAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(PermissionAction.class);

	@Autowired
	private RoleManageService roleManageService;
	@Autowired
	private PermissionManageService permissionManageService;
	
	private String roleId;
	private List<Map> rows;
	private List<Map> roleInfo;
	private List<Map> rolePermissions;
	private String functionIds;

	@Action(value = "query", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rolePermissions" }) })
	public String query() throws ServiceException {
		rolePermissions = permissionManageService.queryRolePermissions(roleId);
		return SUCCESS;
	}
	
	@Action(value = "save", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String save() throws ServiceException {
		permissionManageService.saveRolePermissions(roleId, functionIds);
		return SUCCESS;
	}
	
	public List<Map> getRows() {
		return rows;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getFunctionIds() {
		return functionIds;
	}

	public void setFunctionIds(String functionIds) {
		this.functionIds = functionIds;
	}

	public List<Map> getRoleInfo() {
		return roleInfo;
	}

	public List<Map> getRolePermissions() {
		return rolePermissions;
	}

	public int getTotal() {
		if (rows == null) {
			return 0;
		} else {
			return rows.size();
		}
	}

}
